package cn.donting.jme.edit;

import cn.donting.jme.edit.context.JmeDesktopFXSystem;
import cn.donting.jme.edit.project.Project;
import cn.donting.jme.edit.ui.EditMainApplication;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;


/**
 * 启动入口
 */
public class EditRunApplication extends Application {


    public static void main(String[] args) throws IOException {
        //加载自定义的JmeDesktopFXSystem
        JmeDesktopFXSystem.load();

        EditRunApplication.launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.close();
        openProject();
    }

    private void openProject() throws IOException {
        Stage stage = new Stage();
        FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getClassLoader().getResource("cn/donting/jme/edit/ui/Edit.fxml"));
        BorderPane root = fxmlLoader.load();
        EditMainApplication controller = fxmlLoader.getController();
        Scene scene = new Scene(root, 1500, 900);
        stage.setTitle("jme-edit-v0.0.1");
        stage.setScene(scene);
        stage.show();


        JmeEditSceneApp jmeEditSceneApp = controller.loadProject(Project.openProject(new File(System.getProperty("user.dir") + File.separator + "project-template")));

        stage.addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                jmeEditSceneApp.stop();
            }
        });

    }
}
